﻿using Abp.AutoMapper;
using AVIS.OutBound.CarGroups;
using AVIS.OutBound.Orders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.BookOrders.Cache
{
    [AutoMap(typeof(BookOrder))]
    [Serializable]
    public class BookOrderCacheItem
    {
        public const string CacheName = "AvisAppBookOrderCache";

        /// <summary>
        /// 订单Id
        /// </summary>
        public string OrderId { get; set; }

        /// <summary>
        /// 订单号，由Wizard接口返回（如：37378367CN5） ReservationNumber
        /// </summary>
        public string OrderNo { get; set; }

        /// <summary>
        /// 订单是否已经提交
        /// </summary>
        public bool IsSubmit { get; set; }

        #region 取还车信息

        /// <summary>
        /// 取车时间
        /// </summary>
        public DateTime PickupDateTime { get; set; }

        /// <summary>
        /// 取车门店编号
        /// </summary>
        public string PickUpStoreCode { get; set; }

        /// <summary>
        /// 取车门店名称
        /// </summary>
        public string PickUpStoreName { get; set; }

        /// <summary>
        /// 取车城市编号
        /// </summary>
        public string PickUpCityCode { get; set; }

        /// <summary>
        /// 取车城市名称
        /// </summary>
        public string PickUpCityName { get; set; }

        /// <summary>
        /// 取车的区域编号
        /// </summary>
        public string PickUpRegionCode { get; set; }

        /// <summary>
        /// 还车时间
        /// </summary>
        public DateTime ReturnDateTime { get; set; }

        /// <summary>
        /// 还车门店编号
        /// </summary>
        public string ReturnStoreCode { get; set; }

        /// <summary>
        /// 还车门店名称
        /// </summary>
        public string ReturnStoreName { get; set; }

        /// <summary>
        /// 还车城市编号
        /// </summary>
        public string ReturnCityCode { get; set; }

        /// <summary>
        /// 还车城市名称
        /// </summary>
        public string ReturnCityName { get; set; }

        /// <summary>
        /// 还车的区域编号
        /// </summary>
        public string ReturnRegionCode { get; set; }

        /// <summary>
        /// 租期
        /// </summary>
        public int RentalLength { get; set; }

        /// <summary>
        /// 租期单位（一般为天）
        /// </summary>
        public string RentalUnit { get; set; }
        #endregion

        /// <summary>
        /// 订单总金额
        /// </summary>
        public double Amount { get; set; }

        /// <summary>
        /// 货币单位
        /// </summary>
        public string CurrencyCode { get; set; }

        /// <summary>
        /// 0 门店 1 城市
        /// </summary>
        public string OrderType { get; set; }

        /// <summary>
        /// Avis全球折扣号
        /// </summary>
        public string AWD { get; set; }

        /// <summary>
        /// 会员卡号
        /// </summary>
        public string WizardNum { get; set; }

        /// <summary>
        /// 优惠券号
        /// </summary>
        public string CouponNum { get; set; }

        /// <summary>
        /// 协议号
        /// </summary>
        public string IATA { get; set; }

        /// <summary>
        /// 协议类型
        /// </summary>
        public int IATAType { get; set; }

        /// <summary>
        /// 付款类型1到付2预付
        /// </summary>
        public string PayType { get; set; }

        /// <summary>
        /// 对应上述属性 新预订 1:城市选择 2:门店选择  3:车组选择 4.附件产品选择 5.客户信息输入 6.完成
        /// 修改 0:查询  1:城市选择 2:门店选择  3:车组选择 4.附件产品选择 5.客户信息输入 6.完成
        /// </summary>
        public int OrderStep { get; set; }

        /// <summary>
        /// 套餐价格代码
        /// </summary>
        public string RateCode { get; set; }

        /// <summary>
        /// 预订车组的唯一码
        /// </summary>
        public string SIPPCode { get; set; }

        /// <summary>
        /// 客户选中的车辆装备等增值服务（结构：类型 + 数量）
        /// </summary>
        public Dictionary<string, int> Equipments { get; set; }

        /// <summary>
        /// 下单时间
        /// </summary>
        public DateTime BookingDate { get; set; }

        /// <summary>
        /// 选中的车组信息
        /// </summary>
        public CarGroup CarGroup { get; set; }

        /// <summary>
        /// 用户选择的增值服务数据（结构：类型 + 数量，多个用“;”隔开）
        /// </summary>
        public string EquipList { get; set; }

        /// <summary>
        /// 用户选择的第三方增值服务（JSON结构： [{"type": "-1","productId": "45","productName": "美国WiFi(含夏威夷)","price": "35"}]）
        /// </summary>
        public string ThirdOrders { get; set; }

        #region 客户资料及航班信息

        /// <summary>
        /// 客户的姓
        /// </summary>
        public string Surname { get; set; }

        /// <summary>
        /// 客户的名
        /// </summary>
        public string Firstname { get; set; }

        /// <summary>
        /// 客户地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 客户的邮政编码
        /// </summary>
        public string Postcode { get; set; }

        /// <summary>
        /// 客户所在城市
        /// </summary>
        public string CustomerCity { get; set; }

        /// <summary>
        /// 客户所在的国家名称
        /// </summary>
        public string CountryName { get; set; }

        /// <summary>
        /// 客户所在的国家编号
        /// </summary>
        public string CountryCode { get; set; }

        /// <summary>
        /// 客户的电话号码
        /// </summary>
        public string Telephone { get; set; }

        /// <summary>
        /// 客户的电子邮箱
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// 常旅客协议名称
        /// </summary>
        public string FTNProgramme { get; set; }

        /// <summary>
        /// 常旅客号码
        /// </summary>
        public string FTNNumber { get; set; }

        /// <summary>
        /// 航班号
        /// </summary>
        public string FlightNumber { get; set; }

        /// <summary>
        /// 是否乘航班抵达（不搭乘航班抵达）
        /// </summary>
        public bool IsNoFlightNumber { get; set; }
        #endregion

        #region MapFrom
        /// <summary>
        /// 将预定单信息保存到BookOrderCacheItem中
        /// </summary>
        /// <param name="requestOrder">请求的预定单信息</param>
        /// <returns></returns>
        public BookOrderCacheItem MapFrom(BookOrder requestOrder)
        {
            if (requestOrder == null)
            {
                return this;
            }
            #region 预定第一步 选择门店
            //预定单OrderId
            if (requestOrder.OrderId != null && this.OrderId != requestOrder.OrderId)
            {
                this.OrderId = requestOrder.OrderId;
            }
            //预定单AWD
            if (requestOrder.AWD != null)
            {
                this.AWD = requestOrder.AWD;
            }
            //预定单优惠券号
            if (requestOrder.CouponNum != null)
            {
                this.CouponNum = requestOrder.CouponNum;
            }
            //预定单会员卡号
            if (requestOrder.WizardNum != null)
            {
                this.WizardNum = requestOrder.WizardNum;
            }
            //预定单协议号
            if (requestOrder.IATA != null)
            {
                this.IATA = requestOrder.IATA;
            }
            //预定单协议号类型
            if (requestOrder.IATAType > 0 && requestOrder.IATAType != this.IATAType)
            {
                this.IATAType = requestOrder.IATAType;
            }
            // 订单类型
            if (!string.IsNullOrEmpty(requestOrder.OrderType))
            {
                this.OrderType = requestOrder.OrderType;
            }
            //预定单付款类型
            if (!string.IsNullOrEmpty(requestOrder.PayType))
            {
                this.PayType = requestOrder.PayType;
            }
            //预定单取车时间
            if (requestOrder.PickupDateTime.Ticks > 0)
            {
                this.PickupDateTime = requestOrder.PickupDateTime;
            }
            //预定单换车时间 和 租期
            if (requestOrder.ReturnDateTime.Ticks > 0)
            {
                this.ReturnDateTime = requestOrder.ReturnDateTime;
            }
            //预定单取车门店编号
            if (!string.IsNullOrEmpty(requestOrder.PickUpStoreCode))
            {
                this.PickUpStoreCode = requestOrder.PickUpStoreCode;
            }
            //预定单取车门店编号
            if (!string.IsNullOrEmpty(requestOrder.PickUpStoreName))
            {
                this.PickUpStoreName = requestOrder.PickUpStoreName;
            }
            //预定单换车门店编号
            if (!string.IsNullOrEmpty(requestOrder.ReturnStoreCode))
            {
                this.ReturnStoreCode = requestOrder.ReturnStoreCode;
            }
            //预定单换车门店编号
            if (!string.IsNullOrEmpty(requestOrder.ReturnStoreName))
            {
                this.ReturnStoreName = requestOrder.ReturnStoreName;
            }

            //预定单取车城市编号
            if (!string.IsNullOrEmpty(requestOrder.PickUpCityCode))
            {
                this.PickUpCityCode = requestOrder.PickUpCityCode;
            }
            //预定单取车城市名称
            if (!string.IsNullOrEmpty(requestOrder.PickUpCityName))
            {
                this.PickUpCityName = requestOrder.PickUpCityName;
            }
            //预定单还车城市编号
            if (!string.IsNullOrEmpty(requestOrder.ReturnCityCode))
            {
                this.ReturnCityCode = requestOrder.ReturnCityCode;
            }
            //预定单还车城市名称
            if (!string.IsNullOrEmpty(requestOrder.ReturnCityName))
            {
                this.ReturnCityName = requestOrder.ReturnCityName;
            }

            //预定单取车区域编号
            if (!string.IsNullOrEmpty(requestOrder.PickUpRegionCode))
            {
                this.PickUpRegionCode = requestOrder.PickUpRegionCode;
            }
            //预定单还车区域编号
            if (!string.IsNullOrEmpty(requestOrder.ReturnRegionCode))
            {
                this.ReturnRegionCode = requestOrder.ReturnRegionCode;
            }

            // 预订单的租期
            if (requestOrder.PickupDateTime.Ticks > 0
                && requestOrder.ReturnDateTime.Ticks > 0
                && requestOrder.RentalLength > 0)
            {
                this.RentalLength = requestOrder.RentalLength;
            }

            //预定单租期单位
            if (!string.IsNullOrEmpty(requestOrder.RentalUnit))
            {
                this.RentalUnit = requestOrder.RentalUnit;
            }

            //预定单步骤
            if (requestOrder.OrderStep > 0)
            {
                this.OrderStep = requestOrder.OrderStep;
            }

            //预定下单时间
            if (requestOrder.BookingDate.Ticks > 0)
            {
                this.BookingDate = requestOrder.BookingDate;
            }

            #endregion

            #region 预定第二步 选择车组
            //套餐价格代码
            if (requestOrder.RateCode != null)
            {
                this.RateCode = requestOrder.RateCode;
            }
            //预定车组唯一码
            if (!string.IsNullOrEmpty(requestOrder.SIPPCode))
            {
                this.SIPPCode = requestOrder.SIPPCode;
            }
            //订单总金额
            if (requestOrder.Amount > 0)
            {
                this.Amount = requestOrder.Amount;
            }
            #endregion

            #region 预定第三步 选择增值服务
            if (!string.IsNullOrWhiteSpace(requestOrder.EquipList))
            {
                this.EquipList = requestOrder.EquipList;
            }

            if (!string.IsNullOrWhiteSpace(requestOrder.ThirdOrders))
            {
                this.ThirdOrders = requestOrder.ThirdOrders;
            }
            #endregion

            #region 预定第三步 提交订单
            if (requestOrder.IsSubmit)
            {
                this.IsSubmit = requestOrder.IsSubmit;
            }

            if (!string.IsNullOrEmpty(requestOrder.Surname))
            {
                this.Surname = requestOrder.Surname;
            }

            if (!string.IsNullOrEmpty(requestOrder.Firstname))
            {
                this.Firstname = requestOrder.Firstname;
            }

            if (requestOrder.CountryName != null)
            {
                this.CountryName = requestOrder.CountryName;
            }

            if (requestOrder.CountryCode != null)
            {
                this.CountryCode = requestOrder.CountryCode;
            }

            if (!string.IsNullOrEmpty(requestOrder.Firstname))
            {
                this.Firstname = requestOrder.Firstname;
            }

            if (requestOrder.Address != null)
            {
                this.Address = requestOrder.Address;
            }

            if (requestOrder.Postcode != null)
            {
                this.Postcode = requestOrder.Postcode;
            }

            if (requestOrder.CustomerCity != null)
            {
                this.CustomerCity = requestOrder.CustomerCity;
            }

            if (!string.IsNullOrEmpty(requestOrder.Telephone))
            {
                this.Telephone = requestOrder.Telephone;
            }

            if (!string.IsNullOrEmpty(requestOrder.Email))
            {
                this.Email = requestOrder.Email;
            }

            if (requestOrder.FTNProgramme != null)
            {
                this.FTNProgramme = requestOrder.FTNProgramme;
            }

            if (requestOrder.FTNNumber != null)
            {
                this.FTNNumber = requestOrder.FTNNumber;
            }

            if (requestOrder.FlightNumber != null)
            {
                this.FlightNumber = requestOrder.FlightNumber;
            }

            #endregion


            if (!string.IsNullOrWhiteSpace(requestOrder.OrderNo))
            {
                this.OrderNo = requestOrder.OrderNo;
            }

            return this;
        }
        #endregion
    }
}
